//
//  connection.h
//  api-server
//
//  Created by Peter Emil Jensen on 26/08/2012.
//  Copyright (c) 2012 Peter Emil Jensen. All rights reserved.
//

#ifndef __api_server__connection__
#define __api_server__connection__

#include "json.h"
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class connection : public boost::enable_shared_from_this<connection>, private boost::noncopyable {
public:
    explicit connection(boost::asio::io_service& io_service);
    boost::asio::ip::tcp::socket& socket();
    void start();
    
private:
    void handle_read(const boost::system::error_code& e, std::size_t bytes_transferred);
    void handle_write(const boost::system::error_code& e);
    boost::asio::io_service::strand strand_;
    boost::asio::ip::tcp::socket socket_;
    boost::array<char, 8192> buffer_;
    
    boost::shared_ptr<json> json_ptr;
    
    std::string json_dump;
};

typedef boost::shared_ptr<connection> connection_ptr;

#endif /* defined(__api_server__connection__) */
